<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item :label="t('ustcstudent.fields.accountId')" prop="accountId">
        <el-input
          v-model="formData.accountId"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.accountId')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.infoRuleId')" prop="infoRuleId">
        <el-input
          v-model="formData.infoRuleId"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.infoRuleId')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentPhoto')" prop="studentPhoto">
        <el-input
          v-model="formData.studentPhoto"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentPhoto')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.admissionsPhoto')" prop="admissionsPhoto">
        <el-input
          v-model="formData.admissionsPhoto"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.admissionsPhoto')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.graduationPhoto')" prop="graduationPhoto">
        <el-input
          v-model="formData.graduationPhoto"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.graduationPhoto')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentCode')" prop="studentCode">
        <el-input
          v-model="formData.studentCode"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentCode')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentName')" prop="studentName">
        <el-input
          v-model="formData.studentName"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentName')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentNamePinyin')" prop="studentNamePinyin">
        <el-input
          v-model="formData.studentNamePinyin"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentNamePinyin')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentNameUsed')" prop="studentNameUsed">
        <el-input
          v-model="formData.studentNameUsed"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentNameUsed')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.gender')" prop="gender">
        <el-input
          v-model="formData.gender"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.gender')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.ethnic')" prop="ethnic">
        <el-input
          v-model="formData.ethnic"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.ethnic')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.birthdate')" prop="birthdate">
        <el-date-picker
          v-model="formData.birthdate"
          type="date"
          value-format="x"
          :placeholder="t('common.selectTextPlaceholder') + t('ustcstudent.fields.birthdate')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.nativePlace')" prop="nativePlace">
        <el-input
          v-model="formData.nativePlace"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.nativePlace')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.politicalStatus')" prop="politicalStatus">
        <el-radio-group v-model="formData.politicalStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.identityType')" prop="identityType">
        <el-select
          v-model="formData.identityType"
          :placeholder="t('common.selectTextPlaceholder') + t('ustcstudent.fields.identityType')"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.identityNumber')" prop="identityNumber">
        <el-input
          v-model="formData.identityNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.identityNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.passportNameEnglish')" prop="passportNameEnglish">
        <el-input
          v-model="formData.passportNameEnglish"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcstudent.fields.passportNameEnglish')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.countryRegion')" prop="countryRegion">
        <el-input
          v-model="formData.countryRegion"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.countryRegion')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.marriageStatus')" prop="marriageStatus">
        <el-radio-group v-model="formData.marriageStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.studentOrigin')" prop="studentOrigin">
        <el-input
          v-model="formData.studentOrigin"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentOrigin')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.birthPlace')" prop="birthPlace">
        <el-input
          v-model="formData.birthPlace"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.birthPlace')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.homeAddress')" prop="homeAddress">
        <el-input
          v-model="formData.homeAddress"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.homeAddress')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.residencePlace')" prop="residencePlace">
        <el-input
          v-model="formData.residencePlace"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.residencePlace')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.residenceAddress')" prop="residenceAddress">
        <el-input
          v-model="formData.residenceAddress"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.residenceAddress')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.residenceZipcode')" prop="residenceZipcode">
        <el-input
          v-model="formData.residenceZipcode"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.residenceZipcode')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.onePassCode')" prop="onePassCode">
        <el-input
          v-model="formData.onePassCode"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.onePassCode')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.stationUniversity')" prop="stationUniversity">
        <el-input
          v-model="formData.stationUniversity"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcstudent.fields.stationUniversity')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.stationHome')" prop="stationHome">
        <el-input
          v-model="formData.stationHome"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.stationHome')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.personalPhone1')" prop="personalPhone1">
        <el-input
          v-model="formData.personalPhone1"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone1')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.personalPhone2')" prop="personalPhone2">
        <el-input
          v-model="formData.personalPhone2"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone2')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.personalPhone3')" prop="personalPhone3">
        <el-input
          v-model="formData.personalPhone3"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone3')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.personalEmail1')" prop="personalEmail1">
        <el-input
          v-model="formData.personalEmail1"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmail1')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.personalEmail2')" prop="personalEmail2">
        <el-input
          v-model="formData.personalEmail2"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmail2')"
        />
      </el-form-item>
      <el-form-item
        :label="t('ustcstudent.fields.personalEmailUniversity')"
        prop="personalEmailUniversity"
      >
        <el-input
          v-model="formData.personalEmailUniversity"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmailUniversity')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.masterNumber')" prop="masterNumber">
        <el-input
          v-model="formData.masterNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.masterNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.phdNumber')" prop="phdNumber">
        <el-input
          v-model="formData.phdNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.phdNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.bachelorNumber')" prop="bachelorNumber">
        <el-input
          v-model="formData.bachelorNumber"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.bachelorNumber')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.trainingPeriod')" prop="trainingPeriod">
        <el-input
          v-model="formData.trainingPeriod"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.trainingPeriod')"
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.trainingInstitution')" prop="trainingInstitution">
        <el-input
          v-model="formData.trainingInstitution"
          :placeholder="
            t('common.inputTextPlaceholder') + t('ustcstudent.fields.trainingInstitution')
          "
        />
      </el-form-item>
      <el-form-item :label="t('ustcstudent.fields.remark')" prop="remark">
        <el-input
          v-model="formData.remark"
          :placeholder="t('common.inputTextPlaceholder') + t('ustcstudent.fields.remark')"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UstcStudentApi, UstcStudentVO } from '@/api/core/ustcstudent'
import { FormRules } from 'element-plus'

/** 科大学生信息 表单 */
defineOptions({ name: 'UstcStudentForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  infoRuleId: undefined,
  studentPhoto: undefined,
  admissionsPhoto: undefined,
  graduationPhoto: undefined,
  studentCode: undefined,
  studentName: undefined,
  studentNamePinyin: undefined,
  studentNameUsed: undefined,
  gender: undefined,
  ethnic: undefined,
  birthdate: undefined,
  nativePlace: undefined,
  politicalStatus: undefined,
  identityType: undefined,
  identityNumber: undefined,
  passportNameEnglish: undefined,
  countryRegion: undefined,
  marriageStatus: undefined,
  studentOrigin: undefined,
  birthPlace: undefined,
  homeAddress: undefined,
  residencePlace: undefined,
  residenceAddress: undefined,
  residenceZipcode: undefined,
  onePassCode: undefined,
  stationUniversity: undefined,
  stationHome: undefined,
  personalPhone1: undefined,
  personalPhone2: undefined,
  personalPhone3: undefined,
  personalEmail1: undefined,
  personalEmail2: undefined,
  personalEmailUniversity: undefined,
  masterNumber: undefined,
  phdNumber: undefined,
  bachelorNumber: undefined,
  trainingPeriod: undefined,
  trainingInstitution: undefined,
  remark: undefined,
  mentorId: undefined,
  mentorCode: undefined,
  mentorName: undefined,
  researchDeptId: undefined,
  researchDept: undefined,
  arpNumber: undefined
})
const formRules = reactive<FormRules>({
  studentPhoto: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentPhoto'),
      trigger: 'blur'
    }
  ],
  admissionsPhoto: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.admissionsPhoto'),
      trigger: 'blur'
    }
  ],
  graduationPhoto: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.graduationPhoto'),
      trigger: 'blur'
    }
  ],
  studentCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentCode'),
      trigger: 'blur'
    }
  ],
  studentName: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentName'),
      trigger: 'blur'
    }
  ],
  studentNamePinyin: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentNamePinyin'),
      trigger: 'blur'
    }
  ],
  studentNameUsed: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentNameUsed'),
      trigger: 'blur'
    }
  ],
  gender: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.gender'),
      trigger: 'blur'
    }
  ],
  ethnic: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.ethnic'),
      trigger: 'blur'
    }
  ],
  birthdate: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.birthdate'),
      trigger: 'blur'
    }
  ],
  nativePlace: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.nativePlace'),
      trigger: 'blur'
    }
  ],
  politicalStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.politicalStatus'),
      trigger: 'blur'
    }
  ],
  identityType: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.identityType'),
      trigger: 'change'
    }
  ],
  identityNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.identityNumber'),
      trigger: 'blur'
    }
  ],
  passportNameEnglish: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.passportNameEnglish'),
      trigger: 'blur'
    }
  ],
  countryRegion: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.countryRegion'),
      trigger: 'blur'
    }
  ],
  marriageStatus: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.marriageStatus'),
      trigger: 'blur'
    }
  ],
  studentOrigin: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.studentOrigin'),
      trigger: 'blur'
    }
  ],
  birthPlace: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.birthPlace'),
      trigger: 'blur'
    }
  ],
  homeAddress: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.homeAddress'),
      trigger: 'blur'
    }
  ],
  residencePlace: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.residencePlace'),
      trigger: 'blur'
    }
  ],
  residenceAddress: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.residenceAddress'),
      trigger: 'blur'
    }
  ],
  residenceZipcode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.residenceZipcode'),
      trigger: 'blur'
    }
  ],
  onePassCode: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.onePassCode'),
      trigger: 'blur'
    }
  ],
  stationUniversity: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.stationUniversity'),
      trigger: 'blur'
    }
  ],
  stationHome: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.stationHome'),
      trigger: 'blur'
    }
  ],
  personalPhone1: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone1'),
      trigger: 'blur'
    }
  ],
  personalPhone2: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone2'),
      trigger: 'blur'
    }
  ],
  personalPhone3: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalPhone3'),
      trigger: 'blur'
    }
  ],
  personalEmail1: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmail1'),
      trigger: 'blur'
    }
  ],
  personalEmail2: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmail2'),
      trigger: 'blur'
    }
  ],
  personalEmailUniversity: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.personalEmailUniversity'),
      trigger: 'blur'
    }
  ],
  masterNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.masterNumber'),
      trigger: 'blur'
    }
  ],
  phdNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.phdNumber'),
      trigger: 'blur'
    }
  ],
  bachelorNumber: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.bachelorNumber'),
      trigger: 'blur'
    }
  ],
  trainingPeriod: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.trainingPeriod'),
      trigger: 'blur'
    }
  ],
  trainingInstitution: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.trainingInstitution'),
      trigger: 'blur'
    }
  ],
  remark: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('ustcstudent.fields.remark'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UstcStudentApi.getUstcStudent(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UstcStudentVO
    if (formType.value === 'create') {
      await UstcStudentApi.createUstcStudent(data)
      message.success(t('common.createSuccess'))
    } else {
      await UstcStudentApi.updateUstcStudent(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    infoRuleId: undefined,
    studentPhoto: undefined,
    admissionsPhoto: undefined,
    graduationPhoto: undefined,
    studentCode: undefined,
    studentName: undefined,
    studentNamePinyin: undefined,
    studentNameUsed: undefined,
    gender: undefined,
    ethnic: undefined,
    birthdate: undefined,
    nativePlace: undefined,
    politicalStatus: undefined,
    identityType: undefined,
    identityNumber: undefined,
    passportNameEnglish: undefined,
    countryRegion: undefined,
    marriageStatus: undefined,
    studentOrigin: undefined,
    birthPlace: undefined,
    homeAddress: undefined,
    residencePlace: undefined,
    residenceAddress: undefined,
    residenceZipcode: undefined,
    onePassCode: undefined,
    stationUniversity: undefined,
    stationHome: undefined,
    personalPhone1: undefined,
    personalPhone2: undefined,
    personalPhone3: undefined,
    personalEmail1: undefined,
    personalEmail2: undefined,
    personalEmailUniversity: undefined,
    masterNumber: undefined,
    phdNumber: undefined,
    bachelorNumber: undefined,
    trainingPeriod: undefined,
    trainingInstitution: undefined,
    remark: undefined,
    mentorId: undefined,
    mentorCode: undefined,
    mentorName: undefined,
    researchDeptId: undefined,
    researchDept: undefined,
    arpNumber: undefined
  }
  formRef.value?.resetFields()
}
</script>
